package com.zqsign.core.key.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;

/**
* @ClassName 	: HttpAspect 
* @Description 	: 切面日志收集
*
* @Company 		: zqsign
* @author 		: wx 
* @date 		: 2017年12月20日 下午8:06:39 
*/
@Aspect
@Component
public class HttpAspect {
	
	Logger logger = LoggerFactory.getLogger(HttpAspect.class);
	
	@Pointcut("execution(public * com.zqsign.core.key.controller.*.*(..))")
	public void log(){}
	
	@Before("log()")
	public void doBefore(JoinPoint joinPoint){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
		logger.info("url={},method={},args={}",request.getRequestURL(),request.getMethod(),joinPoint.getArgs());
		
//		System.out.println("class-method:"+joinPoint.getSignature().getDeclaringTypeName()+"---"+joinPoint.getSignature().getName());
		
	}
	
	@AfterReturning(returning = "object",pointcut = "log()")
	public void doAfterReturning(Object object){
		logger.info("response:={}",JSON.toJSONString(object));
//		System.out.println("response:"+JSON.toJSONString(object));
	}

	@After("log()")
	public void doAfter(JoinPoint joinPoint){
		logger.error("{}类的{}方法访问结束了！",joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName());
//		System.out.println(joinPoint.getSignature().getDeclaringTypeName()+" 的 "+joinPoint.getSignature().getName()+"访问结束了！");
	}
}	
